#' ---
#' title: "Whose Dimension is it anyway: CCES Legislator and Citzins Scaling"
#' author: "Lukas F. Stoetzer"
#' ---

#' Script estimates irt models for CCES Study

  # Libraries
  library(pscl)

  # Set values for mcmc
  sample <- 120000
  burnin <- 20000
  thinin <- 100
  
  # Start Time
  start.time <- Sys.time()
  
# CCES 08 ========================
  
# 1.1) Prepare Data  =========

  # Load Data-set
    load("cces2008_rollvote.Rdata")
  
  # Legislators
    rvs <- paste("rv",c(1:9),sep="")
    ld <- as.matrix(rbind(sen110[,rvs],hou110[,rvs]))
  
  # Respondents
    resp <- cces2008
    rd <- as.matrix(resp[,rvs])
  
  # Starting Values to assure converagence
    init_hou  <- ifelse(hou110$party=="Republican", 1, -1) # set republican start value at 1; democrat -1
    init_sen <- ifelse(sen110$party=="R", 1, -1)   # set republican start value at 1; democrat -1
    init_resp <- ifelse(resp$pid == 7, 1, 
                        ifelse(resp$pid== 1, -1, 0)) # set strong republican start value at 1; strong democrat -1; others 0
    init_resp[is.na(init_resp)] <- 0
    
# 1.2) Legislators & Respondent scaling ======

  # Legsilator Based Scaling
   leg_08 <- ideal(rollcall(ld), 
                   maxiter=sample, thin=thinin, burnin=burnin,
                   normalize=T,
                   startvals=list(x= c(init_sen,init_hou)),
                   store.item=TRUE)
  
  # Legsilator Based Scaling
    resp_08 <- ideal(rollcall(rd), 
                     maxiter=sample, thin=thinin, burnin=burnin,
                     normalize=T,
                     startvals=list(x= init_resp),
                     store.item=TRUE)
     

  # 1.4) Post-Estimation ========= 

  # Save
    save(leg_08, resp_08,
         file="res_cces08_scaling.Rdata")
    rm(leg_08,resp_08)
    
  
# 2) CCES 10 ========================
  
  # 2.1) Prepare Data  =========
  
  # Load Data-set
    load("cces2010_rollvote.Rdata")
    
  # Legislators
    rvs <- paste("rv",c(1:4,6:10),sep="")
    ld <- as.matrix(rbind(sen111[,rvs],hou111[,rvs]))
    
  # Respondents
    resp <- cces2010
    rd <- as.matrix(resp[,rvs])
  
  # Starting Values to assure converagence
    init_hou  <- ifelse(hou111$party=="Republican", 1, -1) # set republican start value at 1; democrat -1
    init_sen <- ifelse(sen111$party=="R", 1, -1)   # set republican start value at 1; democrat -1
    init_resp <- ifelse(resp$pid == 7, 1, 
                        ifelse(resp$pid== 1, -1, 0)) # set strong republican start value at 1; strong democrat -1; others 0
    init_resp[is.na(init_resp)] <- 0
    
  # 2.2) Legislators & Respondent scaling ======
  
  # Legsilator Based Scaling
    leg_10 <- ideal(rollcall(ld), 
                    maxiter=sample, thin=thinin, burnin=burnin,
                    normalize=T,
                    startvals=list(x= c(init_sen,init_hou)),
                    store.item=TRUE)
  
  # Legsilator Based Scaling
    resp_10 <- ideal(rollcall(rd), 
                     maxiter=sample, thin=thinin, burnin=burnin,
                     normalize=T,
                     startvals=list(x= init_resp),
                     store.item=TRUE)

    
  # 2.4) Post-Estimation ========= 
  
  # Save
  save(leg_10,resp_10,
       file="res_cces10_scaling.Rdata")
  
  rm(leg_10,resp_10)
  
  
# # 3) CCES 12 ========================
   
   # 2.1) Prepare Data  =========
   
   # Load Data-set
    load("in/cces2012_rollvote.Rdata")
   
   # Legislators
     rvs <- paste("rv",c(1:3,5:10),sep="")
     ld <- as.matrix(rbind(sen112[,rvs],hou112[,rvs]))
     
   # Respondents
     resp <- cces2012
     rd <- as.matrix(resp[,rvs])
   
   # Starting Values to assure converagence
     init_hou  <- ifelse(hou112$party=="Republican", 1, -1) # set republican start value at 1; democrat -1
     init_sen <- ifelse(sen112$party=="R", 1, -1)   # set republican start value at 1; democrat -1
     init_resp <- ifelse(resp$pid == 7, 1, 
                         ifelse(resp$pid== 1, -1, 0)) # set strong republican start value at 1; strong democrat -1; others 0
     init_resp[is.na(init_resp)] <- 0  
     
   # 2.2) Legislators & Respondent scaling ======
   
   # Legsilator Based Scaling
     leg_12 <- ideal(rollcall(ld), 
                     maxiter=sample, thin=thinin, burnin=burnin,
                     normalize=T,
                     startvals=list(x= c(init_sen,init_hou)),
                     store.item=TRUE)
   

   # Legsilator Based Scaling
     resp_12 <- ideal(rollcall(rd), 
                      maxiter=sample, thin=thinin, burnin=burnin,
                      normalize=T, 
                      startvals=list(x= init_resp),
                      store.item=TRUE)
   
     
   # 2.4) Post-Estimation ========= 
   
   # Save
   save(leg_12,resp_12,
        file="res_cces12_scaling.Rdata")
   rm(leg_12,resp_12)
  
  # Time Track
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  print(time.taken)

